﻿using System;
using System.Collections.Generic;
using System.IO;
using DataFiller.DataStorage.Extension.ExcelOperator;
using DataFiller.ExportWithNpoi;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DataFillerTest
{
    [TestClass]
    public class ExportOrganizerXSLXTest
    {
        TestModel Model;

        [TestInitialize]
        public void TestInitialize()
        {
            Model = new TestModel();
            Data1();
            Data2();
        }

        private void Data2()
        {
            Model.Model043 = new Table043
            {
                FormationUnit = "ABC Soft",
                FormationDate = DateTime.Now.ToShortDateString(),
                Unit = "Yuan",
                AssetInformations = new List<Asset>()
            };
            for (int i = 0; i < 1000; i++)
            {
                Asset information = new Asset();
                information.Code = i;
                information.Partner = string.Format("ABC Soft {0}", i / 20);
                information.ModelNumber = string.Format("Specifications Model {0}", i / 20);
                information.MeasurementUnit = string.Format("unit of measure {0}", i);
                information.Type = (AssetType)(i % 4);
                information.Count = i / 100;
                information.Location = string.Format("ABC Soft  {0}", i / 20);
                information.Price = i / 100;
                information.StdName = string.Format("test {0}", i);
                information.Allocation = new AssetAllocation { CurrentAssets = i * 2, FixedAssets = i * 3 };

                Model.Model043.AssetInformations.Add(information);
            }
        }
        private void Data1()
        {
            Model.V1 = "V1";
            Model.V2 = DateTime.Now.ToString();

            Model.Addresses = new List<Address>();
            for (int i = 0; i < 100; i++)
            {
                Model.Addresses.Add(new Address
                {
                    AddressID = i.ToString(),
                    City = "City " + i,
                    ModifiedDate = DateTime.Now.ToString(),
                    PostalCode = "PostalCode " + i,
                    SpatialLocation = "SpatialLocation " + i
                });
            }

            Model.ContactTypes = new List<ContactType>();
            for (int i = 0; i < 50; i++)
            {
                Model.ContactTypes.Add(new ContactType
                {
                    ContactTypeID = i.ToString(),
                    ModifiedDate = DateTime.Now.ToString(),
                    Name = "ContactType " + i
                });
            }

            Model.Persons = new List<Person>();
            for (int i = 0; i < 200; i++)
            {
                Model.Persons.Add(new Person
                {
                    FirstName = "FirstName " + i,
                    NameStyle = i.ToString(),
                    PersonType = i.ToString(),
                    Title = "Title " + i
                });
            }
        }

        [TestMethod]
        public void ExportTest()
        {
            IExcelbook book = new NBook(@"Template\Template.xlsx");
            byte[] resultBytes;
            bool result = ExcelExportOrganizer.ExportExcel(RetrieveModelCallBack, book, out resultBytes);
            File.WriteAllBytes("Result.xlsx", resultBytes);
            //Process.Start("Result.xlsx");
            Assert.IsTrue(result);
        }

        dynamic RetrieveModelCallBack(string data)
        {
            return Model;
        }
    }
}
